package com.ezcoding.controller;

import com.ezcoding.service.UserRelationService;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

@Before(Tx.class)
public class UserRelationController extends Controller {

    /**
     * 查询平台用户
     */
    public void queryPlatformUser() {
        String username = getPara("username");
        String companyId = getPara("companyId");

        renderJson(UserRelationService.getInstance().queryPlatformUser(username, companyId));
    }

    /**
     * 添加平台用户
     */
    public void addPlatformUser() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        //客供关系类型： 1客户 2供应商 3客户供应商
        String type = getPara("type");

        renderJson(UserRelationService.getInstance().addPlatformUser(sourceUsername, targetUsername, type));
    }

    /**
     * 添加非平台用户
     */
    public void addNonPlatformUser() {
        String username = getPara("username");
        String type = getPara("type");

        String nickName = getPara("nickName");
        String contact = getPara("contact");
        String tel = getPara("tel");
        String remark = getPara("remark");
        String primaryDirectory = getPara("primaryDirectory");
        String secondaryDirectory = getPara("secondaryDirectory");

        renderJson(UserRelationService.getInstance().addNonPlatformUser(
                username,
                type,
                nickName,
                contact,
                tel,
                remark,
                primaryDirectory,
                secondaryDirectory));
    }

    /**
     * 获取用户关系
     */
    public void getUserRelations() {
        String username = getPara("username");
        String type = getPara("type");

        renderJson(UserRelationService.getInstance().getUserRelationsByUsernameAndType(username, type));
    }

    /**
     * 获取用户关系详细信息
     */
    public void queryUserRelationDetail() {
        String username = getPara("username");
        String companyId = getPara("companyId");

        renderJson(UserRelationService.getInstance().queryUserRelationDetail(username, companyId));
    }

    /**
     * 为用户建立子账号
     */
    public void buildSubAccount() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String type = getPara("type");

        renderJson(UserRelationService.getInstance().updateUserRelation(
                sourceUsername,
                targetUsername,
                type,
                UserRelationService.BUILD_SUB_ACCOUNT));
    }

    /**
     * 修改用户关系为客供关系
     */
    public void changeUserRelation() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String type = getPara("type");

        renderJson(UserRelationService.getInstance().updateUserRelation(
                sourceUsername,
                targetUsername,
                type,
                UserRelationService.CHANGE_USER_RELATION));
    }

    /**
     * 修改用户关系详细信息
     */
    public void updateUserRelation() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String type = getPara("type");

        String urClientName = getPara("urClientName");
        String urContact = getPara("urContact");
        String urTel = getPara("urTel");
        String urRemark = getPara("urRemark");
        String urCredit = getPara("urCredit");
        String urPrimaryDirectory = getPara("urPrimaryDirectory");
        String urSecondaryDirectory = getPara("urSecondaryDirectory");

        String userNickName = getPara("userNickName");
        String userPosition = getPara("userPosition");
        String userTel = getPara("userTel");
        String userMobile = getPara("userMobile");
        String userQQ = getPara("userQQ");
        String userWechat = getPara("userWechat");

        String companyCompanyName = getPara("companyCompanyName");
        String companyContact = getPara("companyContact");
        String companyTel = getPara("companyTel");
        String companyFax = getPara("companyFax");
        String companyWebsite = getPara("companyWebsite");
        String companyEmail = getPara("companyEmail");

        String companyProvince = getPara("companyProvince");
        String companyCity = getPara("companyCity");
        String companyDistrict = getPara("companyDistrict");
        String companyAddress = getPara("companyAddress");
        String companyAccount = getPara("companyAccount");
        String companyIntroduction = getPara("companyIntroduction");

        String companyTicketCompanyName = getPara("companyTicketCompanyName");
        String companyTaxNum = getPara("companyTaxNum");
        String companyBank = getPara("companyBank");
        String companyTicketAccount = getPara("companyTicketAccount");
        String companyTicketTel = getPara("companyTicketTel");
        String companyTicketAddress = getPara("companyTicketAddress");

        renderJson(UserRelationService.getInstance().updateUserRelation(
                sourceUsername,
                targetUsername,
                type,
                urClientName,
                urContact,
                urTel,
                urRemark,
                urCredit,
                urPrimaryDirectory,
                urSecondaryDirectory,
                userNickName,
                userPosition,
                userTel,
                userMobile,
                userQQ,
                userWechat,
                companyCompanyName,
                companyContact,
                companyTel,
                companyFax,
                companyWebsite,
                companyEmail,
                companyProvince,
                companyCity,
                companyDistrict,
                companyAddress,
                companyAccount,
                companyIntroduction,
                companyTicketCompanyName,
                companyTaxNum,
                companyBank,
                companyTicketAccount,
                companyTicketTel,
                companyTicketAddress));
    }

    /**
     * 上传用户关系
     */
    public void uploadUserRelation() {
        String username = getPara("username");
        String type = getPara("type");
        String relations = getPara("relations");

        renderJson(UserRelationService.getInstance().uploadUserRelation(username, type, relations));
    }

    /**
     * 调整用户关系财务信息 开票 收票 付款 收款
     */
    public void adjustUserRelationFinance() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        String relatedMessageId = getPara("relatedMessageId");
        int relationType = getParaToInt("relationType");
        double oldTicketIn = Double.parseDouble(getPara("oldTicketIn"));
        double oldTicketOut = Double.parseDouble(getPara("oldTicketOut"));
        double oldMoneyIn = Double.parseDouble(getPara("oldMoneyIn"));
        double oldMoneyOut = Double.parseDouble(getPara("oldMoneyOut"));
        double ticketIn = Double.parseDouble(getPara("ticketIn"));
        double ticketOut = Double.parseDouble(getPara("ticketOut"));
        double moneyIn = Double.parseDouble(getPara("moneyIn"));
        double moneyOut = Double.parseDouble(getPara("moneyOut"));

        renderJson(UserRelationService.getInstance().adjustUserRelationFinance(
                sourceUsername,
                targetUsername,
                relatedMessageId,
                relationType,
                oldTicketIn,
                oldTicketOut,
                oldMoneyIn,
                oldMoneyOut,
                ticketIn,
                ticketOut,
                moneyIn,
                moneyOut));
    }

    /**
     * 删除用户关系
     */
    public void deleteUserRelation() {
        String sourceUsername = getPara("sourceUsername");
        String targetUsername = getPara("targetUsername");
        int relationType = getParaToInt("relationType");

        renderJson(UserRelationService.getInstance().deleteUserRelation(
                sourceUsername,
                targetUsername,
                relationType));
    }
}
